package com.xcjw.com.Base;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;

import com.lzy.okhttputils.OkHttpUtils;
import com.xcjw.com.Activity.LoginActivity;
import com.xcjw.com.Hy.DemoHelper;
import com.xcjw.com.View.CustomDialog;

import java.util.ArrayList;
import java.util.Timer;

/**
 * @packname com.xcjw.com.Base
 * @filename Myapplication
 * @date on 2018/11/19 16:26
 *****/
public class Myapplication extends Application {

      private static Myapplication instance;
      private static Timer timer;
      public static Context mcontext;
      public static final String SHARED_NAME = "SP";
      public static SharedPreferences sp;
      public static SharedPreferences.Editor editor;
      public static String currentUserNick = "";
      public static Handler mainHandler;//主线程handle
//      public static IWXAPI mWxApi;
      public static boolean istrue = true;
      private CustomDialog dialog;
      private Dialog niftyDialogBuilder;
      public static ArrayList<Activity>  activities = new ArrayList<>();
      public static Myapplication getInstance() {

            if (instance == null) {
                  instance = new Myapplication();
            }
            return instance;
      }
      public static Timer getTimer() {
            if (timer == null) {
                  timer = new Timer();
            }
            return timer;
      }

      //进行一些初始化操作
      public void setHandler(Handler handler) {
            this.mainHandler = handler;
      }
      public void addActivity(Activity activity) {
            activities.add(activity);
      }
      public  void  exitactivity(){
            for (Activity activity1 : activities) {
                  if (activity1 instanceof LoginActivity){
                  }else {
                        System.out.println(activity1.toString()+"{{{");
                        activity1.finish();
                  }

            }
      }

      @Override
      public void onTerminate() {
            super.onTerminate();




           //System.exit(0);
      }

      // get方法
      public Handler getHandler() {
            return mainHandler;
      }

      @Override
      public void onCreate() {
            super.onCreate();
            //初始化context
            mcontext = this;
            //初始化主线程handle
            sp = getSharedPreferences(SHARED_NAME, MODE_PRIVATE);
            editor = sp.edit();

            OkHttpUtils.getInstance()//
                      .debug("OkHttpUtils")                                              //是否打开调试
                      .setConnectTimeout(OkHttpUtils.DEFAULT_MILLISECONDS)               //全局的连接超时时间
                      .setReadTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS)                  //全局的读取超时时间
                      .setWriteTimeOut(OkHttpUtils.DEFAULT_MILLISECONDS);

            DemoHelper.getInstance().init(mcontext);
            // EMClient.getInstance().addConnectionListener(new MyConnectionListener());
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                  builder.detectFileUriExposure();
            }
            registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
                  @Override
                  public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

                  }

                  @Override
                  public void onActivityStarted(Activity activity) {

                  }

                  @Override
                  public void onActivityResumed(Activity activity) {
                        MyActivityManager.getInstance().setCurrentActivity(activity);
                  }

                  @Override
                  public void onActivityPaused(Activity activity) {

                  }

                  @Override
                  public void onActivityStopped(Activity activity) {

                  }

                  @Override
                  public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

                  }

                  @Override
                  public void onActivityDestroyed(Activity activity) {

                  }
            });


      }

//      private void registToWX() {
//            //AppConst.WEIXIN.APP_ID是指你应用在微信开放平台上的AppID，记得替换。
//            mWxApi = WXAPIFactory.createWXAPI(this, NetUrl.APP_ID, false);
//            // 将该app注册到微信
//            mWxApi.registerApp(NetUrl.APP_ID);
//      }


//      // 实现ConnectionListener接口
//      private class MyConnectionListener implements EMConnectionListener {
//            @Override
//            public void onConnected() {
//
//            }
//
//            @Override
//            public void onDisconnected(final int error) {
//                  if (error == EMError.USER_LOGIN_ANOTHER_DEVICE) {
//                        StackManager.getInstance().currentActivity().runOnUiThread(new Runnable() {
//                              @Override
//                              public void run() {
//                                    showiosDialog();
//                              }
//                        });
//                        // Log.e("hahaha", "onDisconnected: " + "被迫下线");
//                  }
//            }
//      }

//      private void showiosDialog() {
//            dialog = new CustomDialog(getApplicationContext(), R.style.customDialog, R.layout.ios_dialog2);
//            dialog.show();
//            TextView tvContent = (TextView) dialog.findViewById(R.id.textsign);
//            tvContent.setText("另一个账号登录，您被迫下线");
//            TextView tvCancel = (TextView) dialog.findViewById(R.id.tv_cancl);
//            TextView tvOk = (TextView) dialog.findViewById(R.id.tv_bind);
//            tvOk.setOnClickListener(new View.OnClickListener() {
//                  @Override
//                  public void onClick(View view) {
//                        dialog.dismiss();
//                  }
//            });
//            tvCancel.setOnClickListener(new View.OnClickListener() {
//                  @Override
//                  public void onClick(View view) {
//                        dialog.dismiss();
//                  }
//            });
//      }

//      void getnoti() {
//            EaseUI.getInstance().getNotifier().setNotificationInfoProvider(new EaseNotifier.EaseNotificationInfoProvider() {
//                  @Override
//                  public String getDisplayedText(EMMessage message) {
//                        return "你的好基友" + message.getFrom() + "发来了一条消息哦";
//                  }
//
//                  @Override
//                  public String getLatestText(EMMessage message, int fromUsersNum, int messageNum) {
//                        return "你的好基友" + message.getFrom();
//                  }
//
//                  @Override
//                  public String getTitle(EMMessage message) {
//                        return "你有一条新消息";
//                  }
//
//                  @Override
//                  public int getSmallIcon(EMMessage message) {
//                        return 0;
//                  }
//
//                  @Override
//                  public Intent getLaunchIntent(EMMessage message) {
//                        return null;
//                  }
//            });
//      }
}